﻿/*
 *  File: CGetHotelURLs.cs
 *  --------------------- 
 *  This class allows to scrape hotel urls from http://www.hotelscombined.com. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace DataCrawler
{
    public class CGetHotelURLs: CDataCrawler
    {
        // ----- protected methods -----

        // List contains all hotel's url.
        protected List<string> hotelURLs;

        // This method will get hotel urls from html source.
        protected override void Handle()
        {
            hotelURLs = new List<string>();          
            var nodes = document.DocumentNode.SelectNodes(
                    ".//*[@class='hc_sri hc_m_v4']");
            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    try
                    {
                        string url = "http://www.hotelscombined.com/SearchedHotel.aspx?fileName=" + node.Attributes["fn"].Value + "&languageCode=EN";
                        hotelURLs.Add(url);
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }

        // ----- Public methods -----

        public CGetHotelURLs(string url)
            : base(url)
        {
        }

        // For debugging purpose only.
        public override string ToString()
        {
            string result = "";
            if (hotelURLs.Count > 0)
            {
                for (int i = 0; i < hotelURLs.Count; ++i)
                    result += hotelURLs[i] + "\n";
            }
            return result;
        }

        // Properties

        // Return a certain string from hotelURLs.
        public string this[int index]
        {
            get { return hotelURLs[index]; }
        }

        // Return the number of links
        public int Count
        {
            get { return hotelURLs.Count; }
        }
    }
}
